package com.ruoyi.web.controller.ems;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.HUAWEI.EmsBarcodeAttribute;
import com.ruoyi.system.service.IEmsBarcodeAttributeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/4/10 15:45
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@Api(tags = "成品条码打印信息api")
@RequestMapping("/ems/barcodeAttribute")
public class EmsBarcodeAttributeController extends BaseController {


    @Autowired
    IEmsBarcodeAttributeService iEmsBarcodeAttributeService;


    /**
     * 查询批次物料追溯
     */
    @ApiOperation(value = "查询-批次物料追溯")
    @GetMapping("/list")
    public TableDataInfo list(EmsBarcodeAttribute emsBarcodeAttribute) {
        startPage();
        LambdaQueryWrapper<EmsBarcodeAttribute> lqw = new LambdaQueryWrapper<EmsBarcodeAttribute>();
        //工单号
        if (StringUtils.isNotBlank(emsBarcodeAttribute.getPo())) {
            lqw.like(EmsBarcodeAttribute::getPo, emsBarcodeAttribute.getPo());
        }
        //产品编码
        if (StringUtils.isNotBlank(emsBarcodeAttribute.getItemNumber())) {
            lqw.like(EmsBarcodeAttribute::getItemNumber, emsBarcodeAttribute.getItemNumber());
        }
        //产品Sn
        if (StringUtils.isNotBlank(emsBarcodeAttribute.getSn())) {
            lqw.like(EmsBarcodeAttribute::getSn, emsBarcodeAttribute.getSn());
        }
        //上传标识
        if (emsBarcodeAttribute.getUploadFlag() != null) {
            lqw.eq(EmsBarcodeAttribute::getUploadFlag, emsBarcodeAttribute.getUploadFlag());
        }
        //产品SN打印时间
        if (null != emsBarcodeAttribute.getBeginTime()) {
            lqw.apply("created_date>='" + DateTime.of(emsBarcodeAttribute.getBeginTime()).toString(DatePattern.NORM_DATETIME_MS_PATTERN) + "'");
        }
        if (null != emsBarcodeAttribute.getEndTime()) {
            lqw.apply("created_date<='" + DateTime.of(emsBarcodeAttribute.getEndTime()).toString(DatePattern.NORM_DATETIME_MS_PATTERN) + "'");
        }
        if (StringUtils.isNotBlank(emsBarcodeAttribute.getCreatedBy())) {
            lqw.like(EmsBarcodeAttribute::getCreatedBy, emsBarcodeAttribute.getCreatedBy());
        }
        List<EmsBarcodeAttribute> list = iEmsBarcodeAttributeService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 新增批次物料追溯
     */
    @Log(title = "新增批次物料追溯", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增-批次物料追溯")
    @PostMapping
    public AjaxResult add(@RequestBody EmsBarcodeAttribute emsBarcodeAttribute) {
        emsBarcodeAttribute.setCreatedBy(SecurityUtils.getUsername());
        emsBarcodeAttribute.setCreatedDate(new Date());
        return toAjax(iEmsBarcodeAttributeService.save(emsBarcodeAttribute) ? 1 : 0);
    }

    /**
     * 修改批次物料追溯
     */
    @Log(title = "修改批次物料追溯", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改-批次物料追溯")
    @PutMapping
    public AjaxResult edit(@RequestBody EmsBarcodeAttribute emsBarcodeAttribute) {
        emsBarcodeAttribute.setUpdatedBy(SecurityUtils.getUsername());
        emsBarcodeAttribute.setUpdatedDate(new Date());
        return toAjax(iEmsBarcodeAttributeService.updateById(emsBarcodeAttribute) ? 1 : 0);
    }

    /**
     * 删除批次物料追溯
     */
    @Log(title = "删除批次物料追溯", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除-批次物料追溯")
    @DeleteMapping("/{teamIds}")
    public AjaxResult remove(@PathVariable Long[] teamIds) {
        return toAjax(iEmsBarcodeAttributeService.removeByIds(Arrays.asList(teamIds)) ? 1 : 0);
    }
}
